package com.jinyang.jetpackdemo

import android.graphics.Bitmap
import androidx.lifecycle.*
import androidx.multidex.MultiDexApplication
import com.jinyang.jetpackdemo.util.LjyLogUtil
import com.tencent.mmkv.MMKV
import dagger.hilt.android.HiltAndroidApp
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import android.widget.ImageView
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraXConfig
import androidx.startup.AppInitializer
import androidx.work.Configuration
import androidx.work.WorkManager
import com.jinyang.jetpackdemo.util.CheckBitmapHook
import com.jinyang.jetpackdemo.util.LjyToastUtil
import com.jinyang.jetpackdemo.util.application.ApplicationUtil
import com.jinyang.jetpackdemo.util.event.FlowBusInitializer
import leakcanary.LeakCanary
import com.taobao.android.dexposed.DexposedBridge

import com.taobao.android.dexposed.XC_MethodHook.MethodHookParam

import com.taobao.android.dexposed.XC_MethodHook





/**
 * @Author:         LiuJinYang
 * @CreateDate:     2021/8/10
 * @Description:
 */
//4. Hilt 应用类: 所有使用 Hilt 的应用都必须包含一个带有 @HiltAndroidApp 注解的 Application 类。
//@HiltAndroidApp 会触发 Hilt 的代码生成操作，生成的代码包括应用的一个基类，该基类充当应用级依赖项容器。
@HiltAndroidApp
class MyApplication : MultiDexApplication(), CameraXConfig.Provider {


    override fun onCreate() {
        if (false) {
            StrictMode.setThreadPolicy(
                StrictMode.ThreadPolicy.Builder()
                    .detectDiskReads()
                    .detectDiskWrites()
                    .detectNetwork() // or .detectAll() for all detectable problems
                    .penaltyLog()
                    .build()
            )
            StrictMode.setVmPolicy(
                VmPolicy.Builder()
                    .detectLeakedSqlLiteObjects()
                    .detectLeakedClosableObjects()
                    .penaltyLog()
                    .penaltyDeath()
                    .build()
            )
        }
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationLifecycleObserver())
//        WorkManager.initialize(
//            this,
//            Configuration.Builder().build()
//        )
         LjyToastUtil.getInstance().init(this)
        AppInitializer.getInstance(this)
            .initializeComponent(LjyInitializer::class.java)
        val rootDir = MMKV.initialize(this)
        LjyLogUtil.d(rootDir)
        initLeakCanary()
        ApplicationUtil.instance.init(this)
        FlowBusInitializer.init(this)

//        DexposedBridge.hookAllConstructors(ImageView::class.java, object : XC_MethodHook() {
//            @Throws(Throwable::class)
//            override fun afterHookedMethod(param: MethodHookParam) {
//                super.afterHookedMethod(param)
//                DexposedBridge.findAndHookMethod(
//                    ImageView::class.java, "setImageBitmap",
//                    Bitmap::class.java, CheckBitmapHook()
//                )
//            }
//        })
    }

    private fun initLeakCanary() {
//        //要自定义堆转储和分析，
//        LeakCanary.config = LeakCanary.config.copy(retainedVisibleThreshold = 3)
//        //禁用堆转储和分析    AppWatcher.objectWatcher仍然会保持跟踪保留的对象，
//        //并LeakCanary将寻找这些对象当您更改LeakCanary.Config.dumpHeap回true。
//        LeakCanary.config = LeakCanary.config.copy(dumpHeap = false)
//        //隐藏泄漏显示活动启动器图标
        LeakCanary.showLeakDisplayActivityLauncherIcon(false)
    }

    /**
     * [可选] 配置 CameraXConfig.Provider
     */
    override fun getCameraXConfig(): CameraXConfig {
        return Camera2Config.defaultConfig()
    }


}

class ApplicationLifecycleObserver : LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onAppForeground(owner: LifecycleOwner) {
        LjyLogUtil.d("${owner.javaClass.simpleName}:app moved to foreground")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onAppBackground(owner: LifecycleOwner) {
        LjyLogUtil.d("${owner.javaClass.simpleName}:app moved to background")
    }
}